import {
  resource
} from '~/static/resource';
import {
  nav2
} from '~/tools/nav2';
import {
  ui
} from '~/tools/ui';
import {
  dateTool
} from '~/tools/date';
import {
  clipboard
} from '~/tools/clipboard'

Component({
  properties: {
    target: String,
    sessionOpenId: String,
    userId: String,
    from: String,
    autoMsg: Object,
    notifyMobile: String,
    actions: Object,
    chooseTarget: {
      type: Boolean,
      value: false
    },
    bottom: {
      type: String,
      value: '376rpx'
    },
    left: {
      type: String,
      value: '36rpx'
    },
  },
  data: {
    resource,
    newMsgCount: 0
  },
  lifetimes: {
    ready() {

    }
  },
  methods: {
    activate() {
      // console.log('active')
      wx.getClipboardData({
        async success(res) {
          wx.hideToast()
          const data = res.data // clipboard data
          if (clipboard.isValid(data)) {
            clipboard.record(data)
          }
          // if (clipboard.isProcessed(data)) return
          // clipboard.setProcessed(data)

          return

        },
        fail(err) {
          console.error(err)
        }
      })
    },
    async clipboardTap(e) {
      // console.log('i-chat chatTap')
      const list = clipboard.getRecords()
      if (list.length == 0) return

      const actionSheet = {}
      list.forEach(ele => actionSheet[ele.text] = `[${dateTool.format(ele.time, 'MM月dd日 hh:mm')}] ${ele.text}`)
      ui.actionSheet(actionSheet, (desc, data) => {
        // console.log(desc, '//', data)
        clipboard.act(data)
      })
    },
    async nav2Target(target) {
      const {
        sessionOpenId,
        userId,
        from,
        autoMsg,
        notifyMobile
      } = this.properties
      // console.log(target, this.properties)
      if (target == 'self') nav2.chat(sessionOpenId, userId, from, autoMsg) // 自己的聊天室
      else if (target == 'list') nav2.chatList() // 聊天列表
      else if (target == 'others') { // admin或master主动与其他用户聊天
        await this.sendSms2User(notifyMobile)
        nav2.chat(sessionOpenId, userId, from, autoMsg)
      }
    },
    async sendSms2User(notifyMobile) {
      if (!notifyMobile) return

      await wx.cloud.callFunction({
        name: 'sendSmsTc',
        data: {
          templateName: "custom_msg",
          templateParamList: [dateTool.format(new Date().getTime(), "M月d日h点"), "1"],
          phoneNumberList: ["+86" + notifyMobile],
          sessionContext: 'notify order user'
        }
      })
    }
  }
})